<template> 
  <div class="app-container">
     <el-row :gutter="10" class="mb8">
                 <el-col :span="1.5">
                   <el-button
                     type="primary"
                     icon="el-icon-plus"
                     size="mini"
                     @click="handleAdd"
                     v-hasPermi="['sys:role:create']"
                   >新增</el-button>
                 </el-col>
                 <el-col :span="1.5">
                   <el-button
                     type="success"
                     icon="el-icon-edit"
                     size="mini"
                     :disabled="single"
                     @click="handleUpdate"
                     v-hasPermi="['sys:role:update']"
                   >修改</el-button>
                 </el-col>
                 <el-col :span="1.5">
                   <el-button
                     type="danger"
                     icon="el-icon-delete"
                     size="mini"
                     :disabled="multiple"
                     @click="handleDelete"
                     v-hasPermi="['sys:role:delete']"
                   >删除</el-button>
                 </el-col>
                 <el-col :span="1.5">
                   <el-button
                     type="warning"
                     icon="el-icon-download"
                     size="mini"
                     @click="handleExport"
                     v-hasPermi="['system:post:export']"
                   >导出</el-button>
                 </el-col>
               </el-row>
      <el-table ref="productAttrCateTable"
                style="width: 100%"
                :data="list"
                v-loading="listLoading"
                border>
        <el-table-column label="编号" width="100" align="center">
          <template slot-scope="scope">{{scope.row.id}}</template>
        </el-table-column>
        <el-table-column label="类型名称" align="center">
          <template slot-scope="scope">{{scope.row.name}}</template>
        </el-table-column>
        <el-table-column label="属性数量" width="200" align="center">
          <template slot-scope="scope">{{scope.row.attributeCount==null?0:scope.row.attributeCount}}</template>
        </el-table-column>
        <el-table-column label="参数数量" width="200" align="center">
          <template slot-scope="scope">{{scope.row.paramCount==null?0:scope.row.paramCount}}</template>
        </el-table-column>
        <el-table-column label="设置" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="getAttrList(scope.$index, scope.row)">规格列表
            </el-button>
            <el-button
              size="mini"
              @click="getParamList(scope.$index, scope.row)">参数列表
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleUpdate(scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

     <div class="pagination-container">

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="listQuery.pageNum"
          :limit.sync="listQuery.pageSize"
          @pagination="getList"
        />
        </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form ref="productAttrCatForm":model="productAttrCate" :rules="rules" label-width="120px">
        <el-form-item label="类型名称" prop="name">
          <el-input v-model="productAttrCate.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类图标：">
          <single-upload v-model="productAttrCate.pic"></single-upload>
        </el-form-item>
          <el-form-item label="列表样式：">
                      <el-radio-group v-model="productAttrCate.style">
                        <el-radio :label="1">一列一个</el-radio>
                        <el-radio :label="2">一列两个</el-radio>
                          <el-radio :label="3">一列3个</el-radio>
                      </el-radio-group>
                </el-form-item>
                  <el-form-item label="是否展示首页：">
                              <el-radio-group v-model="productAttrCate.showIndex">
                                <el-radio :label="1">展示</el-radio>
                                <el-radio :label="2">不展示</el-radio>
                              </el-radio-group>
                        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('productAttrCatForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
   </div>
</template>
<script>
  import {fetchList,createProductAttrCate,deleteProductAttrCate,updateProductAttrCate} from '@/api/pms/productAttrCate'
  import SingleUpload from '@/components/Upload/singleUpload';
  export default {
    name: 'productAttrCateList',
    components: {SingleUpload},
    data() {
      return {
        list: null,
          // 非单个禁用
                     single: true,
                     // 非多个禁用
                     multiple: true,
                       // 总条数
                      total: 0,
                      isEdit:false,
                       // 弹出层标题
                            title: "",
                     // 是否显示弹出层
                       open: false,
        listLoading: true,
        listQuery: {
          pageNum: 1,
          pageSize: 5
        },
        dialogVisible: false,
        dialogTitle:'',
        productAttrCate:{
          name:'',
          id:null
        },
        rules: {
          name: [
            { required: true, message: '请输入类型名称', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        this.listLoading = true;
        fetchList(this.listQuery).then(response => {
          this.listLoading = false;
          this.list = response.data.records;
          this.total = response.data.total;
        });
      },
      addProductAttrCate() {
        this.dialogVisible = true;
        this.dialogTitle = "添加类型";
      },
      handleSizeChange(val) {
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.listQuery.pageNum = val;
        this.getList();
      },
      handleDelete(row) {
         const ids = row.id || this.ids;
        this.$confirm('是否确认要删除该品牌', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteProductAttrCate(row.id).then(response=>{
            this.$message({
              message: '删除成功',
              type: 'success',
              duration:1000
            });
            this.getList();
          });
        });
      },
      handleUpdate(index, row) {
        this.dialogVisible = true;
        this.dialogTitle = "编辑类型";
        this.productAttrCate.name = row.name;
        this.productAttrCate.id = row.id;
      },
      getAttrList(index, row) {
        this.$router.push({path: '/pms/productAttrList',query:{cid:row.id,cname:row.name,type:0}})
      },
      getParamList(index, row) {
        this.$router.push({path: '/pms/productAttrList',query:{cid:row.id,cname:row.name,type:1}})
      },
      handleConfirm(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = new URLSearchParams();
            data.append("name",this.productAttrCate.name);
data.append("style",this.productAttrCate.style);
data.append("showIndex",this.productAttrCate.showIndex);
data.append("pic",this.productAttrCate.pic);
            if(this.dialogTitle==="添加类型"){
              createProductAttrCate(data).then(response=>{
                this.$message({
                  message: '添加成功',
                  type: 'success',
                  duration:1000
                });
                this.dialogVisible = false;
                this.getList();
              });
            }else{
              updateProductAttrCate(this.productAttrCate.id,data).then(response=>{
                this.$message({
                  message: '修改成功',
                  type: 'success',
                  duration:1000
                });
                this.dialogVisible = false;
                this.getList();
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      /** 新增按钮操作 */
               handleAdd() {
                 this.reset();

                 this.open = true;
                 this.title = "添加角色";
               },
               /** 修改按钮操作 */
               handleUpdate(row) {
                 this.reset();
                 this.isEdit=true;
                 const id = row.id || this.ids

                 getBrand(id).then(response => {
                   this.brand = response.data;
                   this.open = true;
                   this.title = "修改角色";
                 });
               },
                /** 导出按钮操作 */
                   handleExport() {
                     const queryParams = this.queryParams;
                     this.$confirm('是否确认导出所有角色数据项?', "警告", {
                         confirmButtonText: "确定",
                         cancelButtonText: "取消",
                         type: "warning"
                       }).then(function() {
                         return exportRole(queryParams);
                       }).then(response => {
                         this.download(response.msg);
                       }).catch(function() {});
                   },
                // 取消按钮
                   cancel() {
                     this.open = false;
                     this.reset();
                   },
                     // 多选框选中数据
                       handleSelectionChange(selection) {
                         this.ids = selection.map(item => item.id)
                         this.single = selection.length!=1
                         this.multiple = !selection.length
                       },
                   /** 重置按钮操作 */
                       resetQuery() {
                         this.dateRange = [];
                         this.resetForm("queryForm");
                         this.handleQuery();
                       },
                        // 表单重置
                           reset() {

                             this.brand = {
                               id: undefined,
                               name: undefined,

                               sort: 0,
                               status: "0",

                               description: undefined
                             };
                             this.resetForm("form");
                           },
    }
  }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
</style>


